package com.d3.sx_eprotection.controller;

import com.d3.sx_eprotection.entity.*;
import com.d3.sx_eprotection.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grider")
public class GriderController {
    @Autowired
    private GriderService griderService;

    @Autowired
    private SupervisorMesgService supervisorMesgService;
    @Autowired
    private SupervisorService supervisorService;
    @Autowired
    private GriderTestService griderTestService;
    @Autowired
    private AQIlevelService aqIlevelService;

    @Autowired
    private CityService cityService;

    //根据code获取网格员对应的待检测信息
    @GetMapping("/getList")
    public List<Map<String,Object>> getBelong(String code){
        List<Map<String,Object>> result = new ArrayList<>();
        List<SupervisorMesg> List = supervisorMesgService.findBelong(code);
        for (SupervisorMesg mesg:List){
            Map<String,Object> map = new HashMap<>();
            map.put("address",mesg.getAddress());
            map.put("level",mesg.getLevel());
            map.put("message",mesg.getMessage());
            map.put("supervisor_phone",mesg.getSupervisor_phone());
            map.put("date",mesg.getDate());
            map.put("time",mesg.getTime());
            map.put("grider_id",mesg.getGrider_id());
            map.put("status",mesg.getStatus());
            map.put("id",mesg.getId());
            map.put("province",cityService.find(mesg.getProvince()));
            map.put("city",cityService.findCity(mesg.getCity()));
            result.add(map);
        }
        return result;
    }
    //封装浓度指标键入上面的信息
    @GetMapping("/getTMsg")
    public Map<String,Object> getTableMesg(Integer id){
        Map<String,Object> map = new HashMap<>();
        SupervisorMesg message = supervisorMesgService.findone(id).get(0);
        Supervisor supervisor = new Supervisor(supervisorService.getById(message.getSupervisor_phone()));
        map.put("supervisor_name",supervisor.getName());
        map.put("phone",message.getSupervisor_phone());
        map.put("province",cityService.find(message.getProvince()));
        map.put("city",cityService.findCity(message.getCity()));
        map.put("address",message.getAddress());
        String tip = aqIlevelService.getTip(message.getLevel());
        switch (message.getLevel()){
            case 1:
                map.put("estimateLevel","一级"+" "+tip);
                break;
            case 2:
                map.put("estimateLevel","二级"+" "+tip);
                break;
            case 3:
                map.put("estimateLevel","三级"+" "+tip);
                break;
            case 4:
                map.put("estimateLevel","四级"+" "+tip);
                break;
            case 5:
                map.put("estimateLevel","五级"+" "+tip);
                break;
            case 6:
                map.put("estimateLevel","六级"+" "+tip);
                break;
        }
        map.put("message",aqIlevelService.getMsg(message.getLevel()));
        return map;
    }
    @PostMapping("/levelTest")
    public Map<String,Object> levelTest(@RequestBody GriderTest griderTest){
//        System.out.println(griderTest);
        int AQIlevel = -1;
        Map<String,Object> result = new HashMap<>();
        List<AQIlevel> result1=aqIlevelService.SO2Test(griderTest.getSO2());
        result.put("SO2level",result1.get(0).getLevel_id());
        AQIlevel=result1.get(0).getLevel_id();
        List<AQIlevel> result2=aqIlevelService.COTest(griderTest.getCO());
        result.put("COlevel",result2.get(0).getLevel_id());
        AQIlevel = AQIlevel>=result2.get(0).getLevel_id()?AQIlevel:result2.get(0).getLevel_id();
        List<AQIlevel> result3=aqIlevelService.PMTest(griderTest.getPM());
        result.put("PM2.5level",result3.get(0).getLevel_id());
        AQIlevel = AQIlevel>=result3.get(0).getLevel_id()?AQIlevel:result3.get(0).getLevel_id();
        result.put("AQIlevel",AQIlevel);
        return result;
    }
    @PostMapping("/submitMsg")
    public Boolean submit(@RequestBody GriderTest AQImsg){
        Boolean update = supervisorMesgService.updateStatus(AQImsg.getSmesg_id());
        return griderTestService.save(AQImsg)&&update;
    }
}
